/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Notification.java
 *
 * Created on Oct 15, 2012, 4:32:52 PM
 */
package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Vector;

import javax.swing.AbstractListModel;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListModel;

import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import pms.*;

/**
 * 
 * @author moonn52
 */
public class Notification extends javax.swing.JFrame {

	private Employee e;

	/**
	 * Creates new form Notification
	 * 
	 * @param e
	 */
	public Notification(Employee e) {
		this.e = e;

		initComponents();

		getAlerts();

	}

	private void getAlerts() {
		// TODO Auto-generated method stub
		alert = new Vector<Alert>();
		if (e instanceof pms.Supervisor)
			try {
				alert = ((pms.Supervisor) e).getAlerts();
//				System.out.println(alert.size());

			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		updateTable();
	}

	private void updateTable() {
		// // TODO Auto-generated method stub
		String[] headers = { "Employee Name", "Description", "Request Date",
				"Start Date", "End Date" };
		String[][] table = new String[alert.size()][5];

		DateTimeFormatter df = DateTimeFormat.forPattern("yyyy/MM/dd");

		for (int j = 0; j < alert.size(); ++j) {
			table[j][0] = alert.get(j).employeeLastName + ", "
					+ alert.get(j).employeeFirstName + " ("
					+ alert.get(j).employeeID + ")";
			table[j][1] = alert.get(j).description;
			table[j][2] = alert.get(j).requestDate.toString(df);
			table[j][3] = alert.get(j).startDate.toString(df);
			table[j][4] = alert.get(j).endDate.toString(df);
		}

		jtblAlerts.setModel(new javax.swing.table.DefaultTableModel(table,
				headers) {
			boolean[] canEdit = new boolean[] { false, false, false, false,
					false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jScrollPane2 = new javax.swing.JScrollPane();
		jTextArea1 = new javax.swing.JTextArea();
		jbtnApprove = new javax.swing.JButton();
		jbtnDeny = new javax.swing.JButton();
		jsplAlerts = new javax.swing.JScrollPane();
		jtblAlerts = new javax.swing.JTable();

		jTextArea1.setColumns(20);
		jTextArea1.setRows(5);
		jScrollPane2.setViewportView(jTextArea1);

		
		setTitle("Notifications");
		setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
		setLocationByPlatform(true);
		setMinimumSize(new java.awt.Dimension(400, 400));

		jbtnApprove.setText("Approve");
		jbtnApprove.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jbtnApproveActionPerformed(evt);
			}
		});

		jbtnDeny.setText("Deny");
		jbtnDeny.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jbtnDenyActionPerformed(evt);
			}
		});

		jtblAlerts.setAutoCreateRowSorter(true);
		jtblAlerts.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] { { null, null, null, null, null },
						{ null, null, null, null, null },
						{ null, null, null, null, null },
						{ null, null, null, null, null } }, new String[] {
						"Employee Name", "Description", "Request Date",
						"Start Date", "End Date" }) {
			Class[] types = new Class[] { java.lang.String.class,
					java.lang.Object.class, java.lang.Object.class,
					java.lang.Object.class, java.lang.Object.class };
			boolean[] canEdit = new boolean[] { false, false, false, false,
					false };

			public Class getColumnClass(int columnIndex) {
				return types[columnIndex];
			}

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		jtblAlerts.setMinimumSize(new java.awt.Dimension(100, 100));
		jsplAlerts.setViewportView(jtblAlerts);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jsplAlerts,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										524, Short.MAX_VALUE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING,
												false)
												.addComponent(
														jbtnDeny,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														jbtnApprove,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														92, Short.MAX_VALUE))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														jsplAlerts,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														525, Short.MAX_VALUE)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jbtnApprove)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(
																		jbtnDeny)))
								.addContainerGap()));

		pack();
	}// </editor-fold>

	private void jbtnApproveActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:

		int count = jtblAlerts.getSelectedRowCount();
		int[] rows = jtblAlerts.getSelectedRows();
		try {

			int n = JOptionPane.showConfirmDialog(null,
					"Are you sure you want to approve all selected vacation?",
					"Approve?", JOptionPane.YES_NO_OPTION);
			if (n == 0) {
				for (int i = 0; i < count; ++i) {
					if (e instanceof pms.Supervisor)
						((pms.Supervisor) e).approveTimeOff(alert.get(
								rows[count - 1 - i]).alertNumber);
					alert.remove(rows[count - 1 - i]);
					
				}

			}
			
			updateTable();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void jbtnDenyActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		int n = JOptionPane.showConfirmDialog(null,
				"Would you like to review more alerts", "Continue Reviewing?",
				JOptionPane.YES_NO_OPTION);

		if (n == 0) {

		} else {
			this.dispose();
		}
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(Notification.class.getName())
					.log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(Notification.class.getName())
					.log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(Notification.class.getName())
					.log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(Notification.class.getName())
					.log(java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				Employee k = null;

				try {
					k = new Supervisor("th0r", "THORSMASH");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				new Notification(k).setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JTextArea jTextArea1;
	private javax.swing.JButton jbtnApprove;
	private javax.swing.JButton jbtnDeny;
	private javax.swing.JScrollPane jsplAlerts;
	private javax.swing.JTable jtblAlerts;
	Vector<Alert> alert;
	private DefaultListModel lm = new DefaultListModel();
	// End of variables declaration//GEN-END:variables

}
